//
//  TelModel.m
//  EcmDoctor
//
//  Created by zhm on 16/2/18.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "TelModel.h"
#import "NetWorkUtil.h"
#import "UserAskPic.h"

@implementation TelModel

+(Class)picUrl_class {
    return [UserAskPic class];
}

+(void)loginWithDoctor_info_id:(NSString *)infoid Isfinish:(NSString *)isfinish success:(void (^)(NSArray *))success failure:(void (^)(NSError *))failure {
    
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
    //请求参数
    
    req.moduleName = @"DoctorRS";
    req.methodName = @"getOnPhoneInfo";
    [req setValue:infoid forKey:@"doctor_info_id"];
    [req setValue:isfinish forKey:@"isfinish"];
    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
        
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
            
            NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *userAttrs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (error) {
                failure(error);
                return;
            }
            
            if (!userAttrs || userAttrs.count == 0) {
                success([[NSMutableArray alloc] init]);
                return;
            }
            
            NSMutableArray *result = [[NSMutableArray alloc] init];
            for (NSDictionary *dic in userAttrs) {
                TelModel *tel = [[TelModel alloc]initWithDictionary:dic];
                [result addObject:tel];
            }
            
            success(result);
            
        } else {
            
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);
        
    } cache:NO];
    
}

+(void)telDetailWithaskId:(NSString *)askId success:(void (^)(TelModel *))success failure:(void (^)(NSError *))failure {
    NetWorkBaseRequest *req = [[NetWorkBaseRequest alloc] init];
    //请求参数
    
    req.moduleName = @"UserRS";
    req.methodName = @"getOnPhoneDetaileInfoByID";
    [req setValue:askId forKey:@"ask_info_id"];
    
    [NetWorkUtil requestWithPOST:req success:^(NetWorkBaseResponse *responseObject) {
        NSError *error = nil;
        
        if (responseObject.code.integerValue == 1) {
            if (!responseObject.data) {
                success(nil);
                return;
            }
            
            NSData *data = [responseObject.data dataUsingEncoding:NSUTF8StringEncoding];
            NSDictionary *userAttrs = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            if (error) {
                failure(error);
                return;
            }
            
            if (!userAttrs || userAttrs.count == 0) {
                
                failure([NSError errorWithDomain:@"0" code:-1 userInfo:nil]);
                return;
            }
            TelModel *tel = [[TelModel alloc]initWithDictionary:userAttrs];
            
            success(tel);
            
        } else {
            
            error = [NSError errorWithDomain:responseObject.msg code:responseObject.code.integerValue userInfo:nil];
            failure(error);
        }
        
    } failure:^(NSError *error) {
        failure(error);
        
    } cache:NO];
}

@end
